﻿namespace PreGUI.Widgets
{
    using System.Xml;
    using PreGUI.Interfaces;
    using UnityEngine;
    using UnityEngine.UI;
    using PreGUI.Helpers;

    /// <summary>
    /// PreGui Window
    /// </summary>
    public class PGWindow
    {
        public string ControlName = "";
        public string Title;
        public IPGControl[] Children;
        public float Width;
        public float Height;

        public PreGui Gui { get; set; }

        RectTransform rectTransform;
        Image image;
        VerticalLayoutGroup verticalLayoutGroup;

        // Title Bar
        PGWindowTitle titleBar;

        public void InitializeWindow(GameObject gameObject)
        {
            gameObject.AddComponent<CanvasRenderer>();
            rectTransform = gameObject.GetComponent<RectTransform>();
            rectTransform.pivot = new Vector2(0.5f, 0.5f);

            image = gameObject.AddComponent<Image>();
            image.color = Gui.BackgroundColor;
            image.sprite = Gui.ControlSprite;
            image.type = Image.Type.Sliced;
        
            rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Width);
            rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Height);
            rectTransform.position.Set(0f, 0f, 0f);

            verticalLayoutGroup = gameObject.AddComponent<VerticalLayoutGroup>();
            verticalLayoutGroup.childForceExpandWidth = true;
            verticalLayoutGroup.childForceExpandHeight = false;
            verticalLayoutGroup.spacing = 3f;
        
            // Set up the title bar
            var titleBarGameObject = new GameObject("Title Bar");
            titleBarGameObject.AddComponent<VerticalLayoutGroup>().childForceExpandHeight = false;
            titleBarGameObject.transform.SetParent(rectTransform, false);
            titleBar = titleBarGameObject.AddComponent<PGWindowTitle>();
            titleBar.Gui = this.Gui;
            titleBar.TargetWindow = gameObject;
            titleBar.Title = Title;

            var contentArea = new GameObject("Content");
            contentArea.transform.SetParent(gameObject.transform);
            
            var contentLayout = contentArea.AddComponent<VerticalLayoutGroup>();
            contentLayout.padding = new RectOffset(6, 6, 6, 6);

            // Add the window content
            if (Children != null)
            {
                Children.ProcessControls(Gui, contentArea, ControlName);
            }
        
        }

        public static PGWindow LoadFromXml(XmlNode node)
        {
            var result = new PGWindow
            {
                Title = node.Attributes["Title"].Value,
                Width = float.Parse(node.Attributes["Width"].Value),
                Height = float.Parse(node.Attributes["Height"].Value),
            };

            if (node.Attributes["Name"] != null)
                result.ControlName = node.Attributes["Name"].Value;

            result.Children = new IPGControl[node.ChildNodes.Count];
            for (var i = 0; i < node.ChildNodes.Count; i++)
            {
                result.Children[i] = node.ChildNodes.Item(i).XmlProcessChildControl();
            }

            return result;
        }

        public static PGWindow FromResource(string resourcePath)
        {
            return PGXmlLoader.LoadFromXml(Resources.Load<TextAsset>(resourcePath).text);
        }

    }
}
